/*
 * ProductBean.java
 * 
 * Copyright (c) 2011 Administrator. 
 * 
 * This file is part of RMS.
 * 
 * RMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RMS.  If not, see <http ://www.gnu.org/licenses/>.
 */
package com.itrustsolution.rms.managedbean;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import com.itrustsolution.rms.entities.RmsClientDept;
import com.itrustsolution.rms.entities.RmsProduct;
import com.itrustsolution.rms.util.HibernateUtil;
import com.itrustsolution.rms.util.JSFUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Administrator
 */
public class ProductBean {

    // Field
    private HtmlInputText txtId;
    private HtmlInputText txtName;
    private HtmlInputText txtInformation;
    private HtmlCommandButton btnAdd;
    private HtmlCommandButton btnEdit;
    private HtmlCommandButton btnRemove;
    private HtmlCommandButton btnSave;
    private HtmlCommandButton btnCancel;
    private HtmlSelectOneMenu somClientDept;
    
    // List of all product data
    private List<RmsProduct> products = new ArrayList<RmsProduct>();
    // List of all client department data
    private List<SelectItem> clientDepts = new ArrayList<SelectItem>();
    private String selectedClientDept;
    
    /** Creates a new instance of ProductBean */
    public ProductBean() {
    }

    /**
     * Get the table data
     * @return the products
     */
    public List<RmsProduct> getProducts() {
        Session session = null;
        try {
            session = HibernateUtil.getCurrentSession();
            Query query = session.createQuery("FROM RmsProduct WHERE productIsactive = 1");
            products = query.list();

        } catch (HibernateException e) {
        } finally {
        }
        return products;
    }

    /**
     * @return the clientDepts
     */
    public List<SelectItem> getClientDepts() {
        Session session = null;
        try {
            session = HibernateUtil.getCurrentSession();
            Query query = session.createQuery("FROM RmsClientDept WHERE clientDeptIsactive = 1");
            List<RmsClientDept> list = query.list();

            clientDepts = new ArrayList<SelectItem>();
            clientDepts.add(new SelectItem(null, "- Please Select -"));
            for (int i = 0; i < list.size(); i++) {
                clientDepts.add(new SelectItem(list.get(i).getClientDeptId().toString(), list.get(i).getClientDeptName()));
            }
        } catch (HibernateException e) {
        } finally {
        }

        return clientDepts;
    }

    /**
     * When row table selected: 
     * 1. Display the form
     * 2. Show edit and remove button
     * @return nothing
     */
    public void rowSelectionListener(RowSelectorEvent e) {
        System.out.println("--------------------------------------rowSelectionListener start");
        if (!getBtnSave().isRendered()) {
            // Display selected row
            RmsProduct obj = (RmsProduct) products.get(e.getRow());
            getTxtId().setValue(String.valueOf(obj.getProductId()));
            getTxtId().setSubmittedValue(String.valueOf(obj.getProductId()));
            getTxtName().setValue(obj.getProductName());
            getTxtName().setSubmittedValue(obj.getProductName());
            getTxtInformation().setValue(obj.getProductInformation());
            getTxtInformation().setSubmittedValue(obj.getProductInformation());
            getSomClientDept().setValue(obj.getRmsClientDept().getClientDeptId().toString());
            getSomClientDept().setSubmittedValue(obj.getRmsClientDept().getClientDeptId().toString());
            
            System.out.println("--------------------------------------rsk id = " + obj.getProductId());
            System.out.println("--------------------------------------rsk name = " + obj.getProductName());
            System.out.println("--------------------------------------rsk client dept id = " + obj.getRmsClientDept().getClientDeptId().toString());
            
            // Show edit and remove button only when row selected
            getBtnEdit().setRendered(true);
            getBtnRemove().setRendered(true);
        }
    }

    /**
     * When add clicked: 
     * 1. Reset the form
     * 2. Enable the form
     * 3. Show only save and cancel button
     * @return nothing
     */
    public String add_action() {
        System.out.println("ADD ACTION --------------------");
        getTxtId().setValue(null);
        getTxtId().setSubmittedValue(null);
        getTxtName().setValue("");
        getTxtName().setSubmittedValue("");
        getTxtName().setDisabled(false);
        getTxtInformation().setValue("");
        getTxtInformation().setSubmittedValue("");
        getTxtInformation().setDisabled(false);

        getBtnAdd().setRendered(false);
        getBtnEdit().setRendered(false);
        getBtnRemove().setRendered(false);
        getBtnSave().setRendered(true);
        getBtnCancel().setRendered(true);
        return "";
    }

    /**
     * When edit clicked: 
     * 1. Enable the form
     * 2. Show only save and cancel button
     * @return nothing
     */
    public String edit_action() {
        System.out.println("EDIT ACTION --------------------");
        System.out.println("--------------------------------------rsk id = " + getTxtId().getValue());
        getTxtName().setDisabled(false);
        getTxtInformation().setDisabled(false);

        getBtnAdd().setRendered(false);
        getBtnEdit().setRendered(false);
        getBtnRemove().setRendered(false);
        getBtnSave().setRendered(true);
        getBtnCancel().setRendered(true);
        return "";
    }

    /**
     * When remove clicked: 
     * 1. Delete the selected row
     * @return nothing
     */
    public String remove_action() {
        System.out.println("REMOVE ACTION --------------------");
//        Session session = null;
//        RskRiskSeverity obj;
//        String result = "";
//        try {
//            if (getTxtId().getValue() != null) {
//                session = HibernateUtil.getCurrentSession();
//                session.beginTransaction();
//
//                obj = (RskRiskSeverity) session.get(RskRiskSeverity.class, Integer.parseInt((String) getTxtId().getValue()));
//                session.delete(obj);
//                session.getTransaction().commit();
//
//                result = txtName.getValue() + " deleted";
//                JSFUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, result);
//            }
//        } catch (HibernateException e) {
//            result = txtName.getValue() + " failed";
//            JSFUtil.addFacesMessage(FacesMessage.SEVERITY_ERROR, result);
//        } finally {
//        }

        return "";
    }

    /**
     * When save clicked: 
     * 1. Save the data
     * 2. Reset the form
     * @return nothing
     */
    public String save_action() {
//        System.out.println("SAVE ACTION --------------------");
//        Session session = null;
//        RskRiskSeverity obj;
//        String result = "";
//        try {
//            session = HibernateUtil.getCurrentSession();
//            session.beginTransaction();
//
//            if (getTxtId().getValue() == null) { // Add
//                obj = new RskRiskSeverity();
//            } else { // Edit
//                obj = (RskRiskSeverity) session.get(RskRiskSeverity.class, Integer.parseInt((String) getTxtId().getValue()));
//            }
//
//            obj.setRiskSeverityName((String) txtName.getValue());
//            obj.setRiskSeverityDesc((String) txtDescription.getValue());
//            obj.setRiskSeverityIsactive(true);
//            session.saveOrUpdate(obj);
//            session.getTransaction().commit();
//
//            getTxtName().setDisabled(true);
//            getTxtDescription().setDisabled(true);
//            getBtnAdd().setRendered(true);
//            getBtnEdit().setRendered(true);
//            getBtnRemove().setRendered(true);
//            getBtnSave().setRendered(false);
//            getBtnCancel().setRendered(false);
//
//            result = txtName.getValue() + " created";
//            if (getTxtId().getValue() != null) {
//                result = txtName.getValue() + " edited";
//            }
//            JSFUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, result);
//            cancel_action();
//
//        } catch (HibernateException e) {
//            result = txtName.getValue() + " failed";
//            JSFUtil.addFacesMessage(FacesMessage.SEVERITY_ERROR, result);
//        } finally {
//        }
        return "";
    }

    /**
     * When cancel clicked: 
     * 1. Reset the form
     * 2. Disable the form
     * 3. Show only add and cancel button
     * @return nothing
     */
    public String cancel_action() {
        System.out.println("CANCEL ACTION --------------------");

        getTxtName().setValue("");
        getTxtName().setSubmittedValue("");
        getTxtName().setDisabled(true);
        getTxtInformation().setValue("");
        getTxtInformation().setSubmittedValue("");
        getTxtInformation().setDisabled(true);

        getBtnAdd().setRendered(true);
        getBtnEdit().setRendered(false);
        getBtnRemove().setRendered(false);
        getBtnSave().setRendered(false);
        getBtnCancel().setRendered(false);
        return "";
    }

    /**
     * @return the txtId
     */
    public HtmlInputText getTxtId() {
        return txtId;
    }

    /**
     * @param txtId the txtId to set
     */
    public void setTxtId(HtmlInputText txtId) {
        this.txtId = txtId;
    }

    /**
     * @return the txtName
     */
    public HtmlInputText getTxtName() {
        return txtName;
    }

    /**
     * @param txtName the txtName to set
     */
    public void setTxtName(HtmlInputText txtName) {
        this.txtName = txtName;
    }

    /**
     * @return the txtInformation
     */
    public HtmlInputText getTxtInformation() {
        return txtInformation;
    }

    /**
     * @param txtInformation the txtInformation to set
     */
    public void setTxtInformation(HtmlInputText txtInformation) {
        this.txtInformation = txtInformation;
    }

    /**
     * @return the btnAdd
     */
    public HtmlCommandButton getBtnAdd() {
        return btnAdd;
    }

    /**
     * @param btnAdd the btnAdd to set
     */
    public void setBtnAdd(HtmlCommandButton btnAdd) {
        this.btnAdd = btnAdd;
    }

    /**
     * @return the btnEdit
     */
    public HtmlCommandButton getBtnEdit() {
        return btnEdit;
    }

    /**
     * @param btnEdit the btnEdit to set
     */
    public void setBtnEdit(HtmlCommandButton btnEdit) {
        this.btnEdit = btnEdit;
    }

    /**
     * @return the btnRemove
     */
    public HtmlCommandButton getBtnRemove() {
        return btnRemove;
    }

    /**
     * @param btnRemove the btnRemove to set
     */
    public void setBtnRemove(HtmlCommandButton btnRemove) {
        this.btnRemove = btnRemove;
    }

    /**
     * @return the btnSave
     */
    public HtmlCommandButton getBtnSave() {
        return btnSave;
    }

    /**
     * @param btnSave the btnSave to set
     */
    public void setBtnSave(HtmlCommandButton btnSave) {
        this.btnSave = btnSave;
    }

    /**
     * @return the btnCancel
     */
    public HtmlCommandButton getBtnCancel() {
        return btnCancel;
    }

    /**
     * @param btnCancel the btnCancel to set
     */
    public void setBtnCancel(HtmlCommandButton btnCancel) {
        this.btnCancel = btnCancel;
    }

    /**
     * @param products the products to set
     */
    public void setProducts(List<RmsProduct> products) {
        this.products = products;
    }

    /**
     * @return the somClientDept
     */
    public HtmlSelectOneMenu getSomClientDept() {
        return somClientDept;
    }

    /**
     * @param somClientDept the somClientDept to set
     */
    public void setSomClientDept(HtmlSelectOneMenu somClientDept) {
        this.somClientDept = somClientDept;
    }

    /**
     * @return the selectedClientDept
     */
    public String getSelectedClientDept() {
        return selectedClientDept;
    }

    /**
     * @param selectedClientDept the selectedClientDept to set
     */
    public void setSelectedClientDept(String selectedClientDept) {
        this.selectedClientDept = selectedClientDept;
    }
}
